﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using PlugNT.Common;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_FileManager
{
    public partial class FckTemplets : AdminPage
    {
        private string path = "~/tools/editer/fck/fcktemplates.xml";
        private string repath = "~/tools/editer/fck/fcktemplates_back.xml";
        private string action = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            if (!IsPostBack)
            {
                string action = Request.QueryString["action"] == null ? "" : Request.QueryString["action"].Trim();
                if (action != "")
                {
                    PostData(action);
                }
                else
                {
                    InitData();
                }
            }

        }


        private void InitData()
        {
            DataTable dt=new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("title");
            dt.Columns.Add("image");
            dt.Columns.Add("description");
            dt.Columns.Add("html");
            XmlDocument xml = new XmlDocument();
            string filePath=Server.MapPath(path);
            xml.Load(filePath);
            XmlNode root = xml.SelectSingleNode("/Templates");
            int tCount =1;
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name == "Template")
                {
                    DataRow dr=dt.NewRow();
                    XmlAttribute title = n.Attributes["title"];
                    XmlAttribute image = n.Attributes["image"];
                    dr["id"] = tCount.ToString();
                    if (title != null)
                    {
                        dr["title"]=title.Value.Trim();
                    }
                    if (image != null)
                    {
                        dr["image"] = image.Value.Trim();
                    }
                    foreach (XmlNode innern in n.ChildNodes)
                    {
                        if (innern.NodeType != XmlNodeType.Comment && innern.Name == "Description")
                        {
                            dr["description"] = innern.InnerText.Trim();
                        }
                        if (innern.NodeType != XmlNodeType.Comment && innern.Name == "Html")
                        {
                            dr["html"] = innern.InnerText.Trim();
                        }
                    }
                    dt.Rows.Add(dr);
                    tCount++;
                }
            }

            Repeater_admin.DataSource = dt.DefaultView;
            Repeater_admin.DataBind();
        }


        private void PostData(string action)
        {
            
            string title = Request.QueryString["title"] == null ? "" : Request.QueryString["title"].Trim();
            string image = Request.QueryString["image"] == null ? "" : Request.QueryString["image"].Trim();
            string description = Request.QueryString["description"] == null ? "" : Request.QueryString["description"].Trim();
            string html = Request.QueryString["html"] == null ? "" : Request.QueryString["html"].Trim();
            if (action == "add" || action == "edit" || action == "delete" || action=="restore")
            {
                XmlDataDocument xmlDoc = new XmlDataDocument();
                string filePath = Server.MapPath(path);
                xmlDoc.Load(filePath);
                XmlNode root = xmlDoc.SelectSingleNode("/Templates");
                XmlNode node=null;
                switch (action)
                {
                    case "add":
                        node = xmlDoc.SelectSingleNode("/Templates/Template[@title='" + title + "']");
                        if (node == null)
                        {
                            XmlElement xe1 = xmlDoc.CreateElement("Template");   //创建一个节点
                            xe1.SetAttribute("title", title);
                            xe1.SetAttribute("image", image);
                            XmlElement xe1sub1 = xmlDoc.CreateElement("Description");   //创建一个子节点
                            xe1sub1.InnerText = description;
                            xe1.AppendChild(xe1sub1);
                            XmlElement xe1sub2 = xmlDoc.CreateElement("Html");   //创建一个子节点
                            xe1sub2.InnerXml = "<![CDATA[" + html + "]]>";
                            xe1.AppendChild(xe1sub2);
                            root.AppendChild(xe1);      //添加到<Templates>节点中
                            xmlDoc.Save(filePath);
                            Response.Redirect("../Message.aspx?message=Fck模板文件添加成功！&z=yes");
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=Fck模板文件添加失败，该模板文件已存在！&z=no");
                        }
                        break;
                    case "edit":
                        node = xmlDoc.SelectSingleNode("/Templates/Template[@title='" + title + "']");
                        if (node != null)
                        {
                            root.RemoveChild(node); //删除node
                            XmlElement xe1 = xmlDoc.CreateElement("Template");   //创建一个节点
                            xe1.SetAttribute("title", title);
                            xe1.SetAttribute("image", image);
                            XmlElement xe1sub1 = xmlDoc.CreateElement("Description");   //创建一个子节点
                            xe1sub1.InnerText = description;
                            xe1.AppendChild(xe1sub1);
                            XmlElement xe1sub2 = xmlDoc.CreateElement("Html");   //创建一个子节点
                            xe1sub2.InnerXml = "<![CDATA[" + html + "]]>";
                            xe1.AppendChild(xe1sub2);
                            root.AppendChild(xe1);      //添加到<Templates>节点中
                            xmlDoc.Save(filePath);
                            Response.Redirect("../Message.aspx?message=Fck模板文件修改成功！&z=yes");
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=Fck模板文件修改失败，该模板文件不存在！&z=no");
                        }
                        break;
                    case "delete":
                        node = xmlDoc.SelectSingleNode("/Templates/Template[@title='" + title + "']");
                        if (node != null)
                        {
                            root.RemoveChild(node); //删除node
                            xmlDoc.Save(filePath);
                            Response.Redirect("../Message.aspx?message=Fck模板文件删除成功！&z=yes");
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=Fck模板文件修改失败，该模板文件不存在！&z=no");
                        }
                        break;

                    case "restore":
                        string reFilePath=Server.MapPath(repath);
                        System.IO.File.Copy(reFilePath, filePath,true);
                        Response.Redirect("../Message.aspx?message=Fck模板文件还原成功！&z=yes");
                        break;
                    default:
                        Response.Redirect("../Message.aspx?message=Fck模板文件修改失败！&z=no");
                        break;
                }
            }
        }

    }
}
